//
//  SubmitOrderBottomTool.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/8/25.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "SubmitOrderBottomTool.h"
@interface SubmitOrderBottomTool()

@property(nonatomic,strong)UILabel * l1;
@end
@implementation SubmitOrderBottomTool
-(id)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.submitOrder = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.width*0.3, self.height)];
        self.submitOrder.backgroundColor = RGBColor(238,151,36);
        [self.submitOrder setTitle:@"提交订单" forState:UIControlStateNormal];
        [self.submitOrder setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.submitOrder addTarget:self action:@selector(submitButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        self.submitOrder.titleLabel.font = [UIFont systemFontOfSize:17];
        [self addSubview:self.submitOrder];
        
        self.l1 = [[UILabel alloc] initWithFrame:CGRectZero];
        self.l1.text = @"总金额:";
        self.l1.textColor = RGBColor(85,85,85);
        self.l1.font = [UIFont systemFontOfSize:12];
        [self addSubview:self.l1];
        
        self.totalPrice = [[UILabel alloc] initWithFrame:CGRectZero];
        self.totalPrice.textColor = RGBColor(64	,170,57);
        
        [self addSubview:self.totalPrice];
        
        
        self.freightLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.freightLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:self.freightLabel];
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(nothing)]];
    }
    return self;
}


-(void)layoutSubviews{
    [super layoutSubviews];
    self.submitOrder.x = self.width - self.submitOrder.width;
    
    self.totalPrice.x = self.width - self.submitOrder.width - self.totalPrice.width;
    self.totalPrice.y = 0;
    
    self.l1.size = [self.l1.text sizeInAre:MaxSize font:self.l1.font];
    self.l1.x = self.totalPrice.left - self.l1.width-globalMargin;
    self.l1.centerY = self.totalPrice.centerY;
    
    self.freightLabel.size = [self.freightLabel.text sizeInAre:MaxSize font:self.freightLabel.font];
    self.freightLabel.x = self.submitOrder.left - self.freightLabel.width-globalMargin;
    self.freightLabel.centerY = (self.height - self.totalPrice.botton)*0.5+self.totalPrice.botton;
    
    
}


-(void)setCount:(NSInteger)count{
}

-(void)setTotalPrice:(CGFloat)price freight:(CGFloat)freight{
    NSString * priceStr = [NSString stringWithFormat:@"¥%.2f",price];
    NSArray * stArray = [priceStr componentsSeparatedByString:@"."];
    NSRange range1 = NSMakeRange(0, [stArray.firstObject length]);
    NSRange range2 = NSMakeRange([stArray.firstObject length]+1, 2);
    NSMutableAttributedString * mstr = [[NSMutableAttributedString alloc] initWithString:priceStr];
    [mstr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]} range:range1];
    [mstr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} range:range2];
    self.totalPrice.attributedText = mstr;
    self.totalPrice.size = mstr.size;
    self.totalPrice.width+=10;
    
    self.freightLabel.text = [NSString stringWithFormat:@"(含运费:%.0f元)",freight];
    
    [self setNeedsLayout];
}
-(void)nothing{
    
}

-(void)submitButtonClick:(UIButton*)button{
    if(self.delegate&&[self.delegate respondsToSelector:@selector(SubmitOrderBottomTool:didClickSubmitButton:)]){
        [self.delegate SubmitOrderBottomTool:self didClickSubmitButton:button];
    }
}
@end
